package api
{
	import api.com.odnoklassniki.Odnoklassniki;
	import api.com.odnoklassniki.sdk.friends.Friends;
	import api.com.odnoklassniki.sdk.photos.Photos;
	
	import api_servers.api_externalserver;

	public class api_okserver extends api_externalserver {
		public static const SECRETS: Object = {
			'6278400': '0C8FBEF24707763ECF000635',
			'CBAOMQFBABABABABA': '0C8FBEF24707763ECF000635',
			'34205952': '776438E5F15FA66D2912F3C2' // dev
		};
		
		public function api_okserver(a_sroot:Object, a_vars: Object) {
			super(a_sroot, a_vars.api_server, a_vars.uid, a_vars.app_id, secrets[a_vars.app_id], 0);
			vars = a_vars;
		}
		
		override public function convertVars():Object {
			return {
				preview_url		: {name:'src_small'},
				standard_url	: {name:'src'},
				gender			: {name:'sex', cnv: function(val: String): uint {
							return (val=='male')?0:1; // Приведение к стандарту моего мира: 0- мужчина, 1-женщина
						}
				},
				pic_2			: {name: 'pic'},
				pic_3			: {name: 'pic_small'},
				pic_4			: {name: 'pic_big'},
				name			: {name: 'nick'},
				url_profile		: {name: 'link'}
			}
		}
		
		public function get secrets() : Object {
			return SECRETS
		}
		
		protected function cHandle(_handle: Function, property: String): Function {
			return function(data: Object): void {
				if (!pj.checkErrorData(data)) {
					argConvert(data[property])
					_handle(data[property]);
				}
			}
		}
		
		public function getAlbums(uid: String, handler_: Function, onError: Function=null): void {
			Photos.getAlbums(cHandle(handler_, 'albums'), uid);
		}
		
		public function getPhotos(uid: String, aid: String, handler_: Function): void {
			Photos.getUserAlbumPhotos(aid, cHandle(handler_, 'photos'), uid);
		}
		
	    public function getAllFriends(uid: String, handler_:*, queryID:String='getAllFriends'): void {
	    	if (_cache[queryID]) handler_(_cache[queryID]);
	    	else {
				Friends.get(function(a_data: Array): void {
					getProfiles(a_data.join(','), handler_, '', queryID);
				}, uid);
	    	}
	    }

	    override public function getProfiles(a_uids: String, handler_:*, a_fields: String='', queryID:String=''): void {
	    	if (queryID && _cache[queryID]) handler_(_cache[queryID]);
	    	else {
		    	a_fields = (a_fields)?a_fields:'uid,first_name,last_name,name,gender,birthday,age,url_profile,current_location,pic_2,pic_3,pic_4';
				Odnoklassniki.callRestApi("users.getInfo", function(data: Object): void {
						if (!pj.checkErrorData(data)) {
							argConvert(data);
							handler_(data);
							if (queryID) _cache[queryID] = data;
						}
					}, 
					Odnoklassniki.getSendObject({uids:a_uids, fields:a_fields, emptyPictures:true})
				);
	    	}
	    }
	    
	    public function get pj(): pj_ok {
	    	return sroot as pj_ok;
	    }
	}
}